<?php
/*
* 管理员功能
* */
namespace Admin\Controller;
class AdminController extends CommonController{

    public function index(){
        $get = I('get.');
        $where = array('type'=>1);
        if(!empty($get['username'])){
            $where['username'] = trim($get['username']);
        }
        $userM = M('user');
        $count = $userM->where($where)->count();
        $page = new \Think\Page($count, 10);
        $list = $userM->where($where)
            ->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('list',$list);
        $this->assign('_page',$page->show());
        $this->display();
    }


    /*
     * 添加管理员
     * */
    public function admin_add(){
        if(IS_POST && IS_AJAX){
            $post = I('post.');
            if($post['option'] != 'add' && $post['option']!= 'update'){
                self::response('400','出错了');
            }
            if(empty($post['id']) && $post['option'] == 'update'){
                self::response('400','出错了');
            }
            if(empty($post['username'])){
                self::response('400','请输入账号');
            }
            if(empty($post['mobile'])){
                self::response('400','请输入绑定的手机号');
            }
            if(empty($post['password']) && $post['option'] == 'add'){
                self::response('400','请输入密码');
            }
            $userM = M('user');

            //判断账号是否已存在
            $post['username'] = trim($post['username']);
            $user = $userM->field('id')->where(array('username'=>$post['username']))->find();
            if(!empty($user) && $post['option'] == 'add'){
                self::response('400','账号已存在，请重新输入');
            }
            if(!empty($user) && $post['id'] != $user['id'] && $post['option'] == 'update'){
                self::response('400','账号已存在，请重新输入');
            }
            //判断绑定的号码是否已存在
            $post['mobile'] = trim($post['mobile']);
            $user = $userM->field('id')->where(array('mobile'=>$post['mobile']))->find();
            if(!empty($user) && $post['option'] == 'add'){
                self::response('400','手机号已存在，请重新输入');
            }
            if(!empty($user) && $post['id'] != $user['id'] && $post['option'] == 'update'){
                self::response('400','手机号已存在，请重新输入');
            }


            $save = array();
            $save['username'] = $post['username'];
            if(!empty($post['password'])){
                $save['salt'] = self::rand_code(16);
                $save['password'] = md5($post['password'].$save['salt']);
            }
            $save['status'] = 1;
            $save['mobile'] = $post['mobile'];
            $save['updated_time'] = NOW_TIME;
            if($post['option'] == 'add'){
                $save['type'] = 1;
                $save['created_time'] = NOW_TIME;
                $userM->add($save);
            }elseif($post['option'] == 'update') {
                $userM->where(array('id'=>$post['id']))->save($save);
            }
            self::response('200','保存成功', array('redirect'=>U('Admin/index')));
        }

        $get = I('get.');
        $option = empty($get['option']) ? 'add' : $get['option'];
        if($option == 'update' && empty($get['id'])){
            self::back();
        }
        if($option == 'update'){
            $userM = M('user');
            $list = $userM->where(array('id'=>$get['id']))->find();
        }

        $this->assign('list', $list);
        $this->assign('option', $option);
        $this->display();
    }

    /*
    * 删除管理员
    * */
    public function admin_del(){
        if(IS_POST && IS_AJAX){
            $id = I('post.id');
            if(empty($id)){
                self::response('400','id为空');
            }
            $userM = M('user');
            $userM->delete($id);
            self::response('200', '操作成功');
        }
    }


    /*
    * 系统配置表
    * */
    public function sys_config(){

        if(IS_POST && IS_AJAX){
            $post = I('post.');
            foreach($post as $key=>$value){
                $post[$key] = trim($value);
            }
            $post['message'] = empty($post['message']) ? 0 : $post['message'];
            $post['notice'] = empty($post['notice']) ? 0 : $post['notice'];

            $sys_configM = M('sys_config');
            $sys_config = $sys_configM->where(array('id'=>1))->find();
            $post = serialize($post);
            if(empty($sys_config)){
                $sys_configM->add(array('sys_config'=>$post));
            }else{
                $sys_configM->where(array('id'=>1))->save(array('sys_config'=>$post));
            }
            self::response('200','保存成功');
        }

        $this->display();
    }
}